package com.gyf.crm.web.controller;

import com.gyf.crm.model.CUDResult;
import com.gyf.crm.model.CourseOrder;
import com.gyf.crm.model.PageResult;
import com.gyf.crm.model.Student;
import com.gyf.crm.service.ICourseOrderService;
import com.gyf.crm.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import java.util.concurrent.locks.ReentrantLock;

@Controller
@RequestMapping("courseorder")
public class CourseOrderController {
    @Autowired
    private ICourseOrderService courseOrderService;

    @RequestMapping("index")
    public String index(){
        return "courseorder/index";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult<CourseOrder> list(CourseOrder condition,int page,int limit){
        System.out.println(condition);
        return courseOrderService.findPageResult2(condition,page,limit);
    }


    @RequestMapping("add")
    public String add(){
        return "courseorder/add";
    }
    @RequestMapping("edit")
    public String edit(Model model,String order_id){
        CourseOrder courseOrder = courseOrderService.findOrderById(order_id);
        model.addAttribute("order",courseOrder);
        return "courseorder/edit";
    }


    @RequestMapping("save")
    @ResponseBody
    public CUDResult save(CourseOrder order){
        CUDResult  result = new CUDResult();
        System.out.println(order);
        if(StringUtils.isEmpty(order.getOrder_id())){
            System.out.println("insert");
            courseOrderService.save(order);
        }else{
            System.out.println("update");
            courseOrderService.update(order);
        }
        //
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public CUDResult delete(String order_id){
        CUDResult  result = new CUDResult();
        System.out.println(order_id);
        courseOrderService.delete(order_id);
        return result;
    }

    @RequestMapping("detail")
    public String detail(Model model,String order_id){
        System.out.println(order_id);
        CourseOrder courseOrder = courseOrderService.findOrderById(order_id);
        model.addAttribute("order",courseOrder);
        System.out.println(courseOrder);
        return "courseorder/detail";
    }
}
